/* global window */
import modelExtend from 'dva-model-extend'
import {pageModel} from 'utils/model'
import * as dictionaryService from 'services/dictionary'
import * as customerService from '../../customer/services/customer'
import {create} from '../../orders/services/allOrder'
import {queryById} from 'services/order'
import pathToRegexp from 'path-to-regexp'
import * as employeeService from "../../employee/services/employees"
import {message} from 'antd'

const {queryAddress} = customerService

export default modelExtend(pageModel, {
  namespace: 'createOrder',

  state: {
    current: {
      customer: {},
    },
    formType: 'create',
    products: [],
    productModalVisible: false,
    addressModalVisible: false,
    editing: false,
    expressType: [],
    addressList: [],
    depotList: [],
    prepayType: [],
    customerChannel: [],
    empDataSource: [],
    route: '',
  },

  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({pathname}) => {
        const match = pathToRegexp('/orderForm/:id').exec(pathname)
        if (match) {
          dispatch({type: 'init', payload: {route: `/orderForm/${match[1]}`}})
          dispatch({
            type: 'searchEmp',
            payload: {
              key: 'any',
            },
          })
          dispatch({
            type: 'queryAddress',
            payload: {
              customerId: match[1],
            },
          })
        }
      })
    },
  },

  effects: {

    * findById ({payload = {}}, {call, put}) {
      const {code, msg, data} = yield call(queryById, payload)
      if (code === 0) {
        const {address} = data
        if (address) {
          address['isNew'] = true
        }
        yield put({
          type: 'updateState',
          payload: {
            id: data.id,
            current: data,
          },
        })
      } else {
        message.error(msg)
      }
    },

    * create ({payload = {}}, {call, put}) {
      const response = yield call(create, payload)
      const {code, msg} = response
      if (code === 0) {
        message.success(msg)
      } else {
        message.error(msg)
      }
    },

    * searchEmp ({payload = {}}, {call, put}) {
      const response = yield call(employeeService.searchEmp, payload)
      const {data} = response
      if (response && data) {
        yield put({
          type: 'updateState',
          payload: {
            empDataSource: data,
          },
        })
      }
    },

    * queryAddress ({payload = {}}, {call, put}) {
      const response = yield call(queryAddress, payload)
      const {code, msg, data} = response
      if (code === 0 && data) {
        yield put({
          type: 'updateState',
          payload: {
            addressList: data,
          },
        })
      } else {
        throw {code, message: msg}
      }
    },

    * init ({payload = {}}, {call, put}) {
      const expressTypeResp = yield call(dictionaryService.queryBySn, {sn: 'express_type'})
      const depotResp = yield call(dictionaryService.queryBySn, {sn: 'depot'})
      const prepayTypeResp = yield call(dictionaryService.queryBySn, {sn: 'prepay_type'})
      const customerChannelResp = yield call(dictionaryService.queryBySn, {sn: 'customer_channel'})
      yield put({
        type: 'updateState',
        payload: {
          expressType: expressTypeResp.data,
          depotList: depotResp.data,
          prepayType: prepayTypeResp.data,
          customerChannel: customerChannelResp.data,
          ...payload,
        },
      })
    },
  },

  reducers: {

    updateState (state, {payload}) {
      return {...state, ...payload}
    },

    saveDetail (state, {payload}) {
      return {...state, ...payload}
    },

    showProductModal (state, {payload}) {
      return {...state, ...payload, productModalVisible: true}
    },

    showAddrModal (state, {payload}) {
      return {...state, ...payload, addressModalVisible: true}
    },

    hideProductModal (state) {
      return {...state, productModalVisible: false}
    },

    hideAddrModal (state, {payload}) {
      return {...state, ...payload, addressModalVisible: false}
    },


  },
})
